﻿#include "Frame_TurnTableStateShower.h"

#include <qpainter.h>

Frame_TurnTableStateShower::Frame_TurnTableStateShower(QWidget* parent)
  : QFrame(parent)
{
  for (int i = 0; i < imageWidth; ++i)
    {
      QList<int>* temp = new QList<int>();
      for (int j = 0; j < imageHeight; ++j)
        {
          temp->append(Write);
        }
      imageMatrix.append(temp);
    }
  label_topMark = new QLabel(this);
  label_midMark = new QLabel(this);
  label_botMark = new QLabel(this);
  label_redLinePosition = new QLabel(this);
  label_greenLineRate = new QLabel(this);
  label_redLinePosition->resize(70, 20);
  label_greenLineRate->resize(70, 20);
  label_redLinePosition->setText(QStringLiteral("<html><head/><body><p><span style=\" color:red; \">红线:位置</span></p></body></html>"));
  label_greenLineRate->setText(QStringLiteral("<html><head/><body><p><span style=\" color:green; \">绿色:速率</span></p></body></html>"));
  label_topMark->setText(QStringLiteral("360°"));
  label_midMark->setText(QStringLiteral("0°"));
  label_botMark->setText(QStringLiteral("-360°"));
}

Frame_TurnTableStateShower::~Frame_TurnTableStateShower()
{
  while (!imageMatrix.isEmpty())
    {
      delete imageMatrix.takeFirst();
    }
}

void Frame_TurnTableStateShower::paintNewState(int position, int rate, int targetRate)
{
  delete imageMatrix.takeFirst();
  QList<int>* temp = new QList<int>();
  for (int j = 0; j < imageHeight; ++j)
    {
      temp->append(Write);
    }
  int redIndex = (((360 - position) * imageHeight) / 720);
  (*temp)[(redIndex == 0 ? 0 : redIndex - 1)] = Red;
  int absoluteTargetRate = (targetRate > 0 ? targetRate : -targetRate);
  int greenIndex = (((absoluteTargetRate - rate) * imageHeight) / (absoluteTargetRate * 2));
  (*temp)[(greenIndex == 0 ? 0 : greenIndex - 1)] = Green;
  imageMatrix.append(temp);
  update();
}

void Frame_TurnTableStateShower::paintEvent(QPaintEvent* event)
{
  paintImage();
  QFrame::paintEvent(event);
}
void Frame_TurnTableStateShower::paintImage()
{
  label_redLinePosition->move(width() - label_redLinePosition->width(), 0);
  label_greenLineRate->move(width() - label_greenLineRate->width(), label_redLinePosition->height());
  label_topMark->move(0, getTopPos() - label_topMark->height());
  label_midMark->move(0, getMidPos() - label_midMark->height());
  label_botMark->move(0, getBotPos() - label_botMark->height());
  QPainter painter(this);
  QPen pen(Qt::white);
  QBrush brush(Qt::white);
  painter.setPen(pen);
  painter.setBrush(brush);
  painter.drawRect(0, 0, width(), height());
  pen.setColor(Qt::black);
  brush.setColor(Qt::black);
  pen.setStyle(Qt::DashLine);
  painter.setPen(pen);
  painter.setBrush(brush);
  painter.drawLine(0, getMidPos(), width(), getMidPos());
  painter.drawLine(0, getTopPos(), width(), getTopPos());
  painter.drawLine(0, getBotPos(), width(), getBotPos());
  QPen pen_red(Qt::red);
  QBrush brush_red(Qt::red);
  QPen pen_green(Qt::darkGreen);
  QBrush brush_green(Qt::darkGreen);
  int i = 0;
  for (QList<int>* temp : imageMatrix)
    {
      for (int j = 0; j < temp->size(); ++j)
        {
          int x = (i * width()) / imageWidth;
          int y = topSpace + ((j * (height() - topSpace - bottomSpace)) / imageHeight);
          switch ((*temp)[j])
            {
            case Red:
              painter.setPen(pen_red);
              painter.setBrush(brush_red);
              painter.drawRect(x, y, 1, 1);
              break;
            case Green:
              painter.setPen(pen_green);
              painter.setBrush(brush_green);
              painter.drawRect(x, y, 1, 1);
              break;
            }
        }
      ++i;
    }
}
int Frame_TurnTableStateShower::getMidPos()
{
  return  (height() - topSpace - bottomSpace) / 2 + topSpace;
}
int Frame_TurnTableStateShower::getTopPos()
{
  return topSpace;
}
int Frame_TurnTableStateShower::getBotPos()
{
  return height() - bottomSpace;
}
